import { Injectable } from '@nestjs/common';
import { In, Like, Repository } from 'typeorm';
import { ResultData } from '@/common/utils/result';
import { instanceToPlain } from 'class-transformer';
import { InjectRepository } from '@nestjs/typeorm';
import { Config } from '@/modules/system/config/entities/config.entity';

@Injectable()
export class ConfigService {
    
    constructor(
        @InjectRepository(Config)
        private readonly ConfigRepository: Repository<Config>,
    ) {
    }
    
    
    async create(createDto) {
        return ResultData.ok(instanceToPlain(await this.ConfigRepository.save(createDto)));
    }
    
    async findAll(findAllDto) {
        const { page, size, username, status } = findAllDto;
        const take = size || 15;
        const skip = ((page || 1) - 1) * take;
        const where = {
            ...(status === 0 || status ? { status } : null),
            ...(username ? { username: Like(`%${username}%`) } : null),
        };
        return ResultData.rowOk(instanceToPlain(await this.ConfigRepository.findAndCount({
            order: {
                id: 'DESC',
            },
            // where,
            skip,
            take,
        })));
    }
    
    async findOne(id: number) {
        return ResultData.ok(instanceToPlain(await this.ConfigRepository.findOneBy({ id })));
    }
    
    // async findOneByAppType(appType: number) {
    //     return ResultData.ok(instanceToPlain(await this.SysConfigRepository.findOne({
    //         where: { appType },
    //     })));
    // }
    
    async update(id: number, updateDto) {
        return ResultData.ok(await this.ConfigRepository.update(id, updateDto), '更新成功');
    }
    
    async removeByIds({ ids }) {
        return ResultData.ok(await this.ConfigRepository.delete({ id: In(ids) }), '删除成功');
    }
    
    async remove(id: number) {
        return ResultData.ok(await this.ConfigRepository.delete(id), '删除成功');
    }
}
